/**
 * @file tkl_zll_commissioning.h
 * @brief Common process - zll commissioning network
 * @version 1.0.0
 * @date 2021-11-16
 *
 * @copyright Copyright 2021-2022 Tuya Inc. All Rights Reserved.
 *
 */

#ifndef __TKL_ZLL_COMMISSIONING_H__
#define __TKL_ZLL_COMMISSIONING_H__

#ifdef __cplusplus
	extern "C" {
#endif

#include "tuya_cloud_types.h"
#include "tuya_zigbee_stack.h"


/*
 *  zll target status
 */
typedef enum {
    TKL_ZLL_TARGET_IDLE = 0,    ///< zll target idle 
    TKL_ZLL_TARGET_LEAVE,       ///< zll target leave by initiator
    TKL_ZLL_TARGET_JOINED,      ///< zll target joined to a zll network
} TKL_ZLL_TARGET_STATUS_E;

/*
 *  zll initiator scan type
 */
typedef enum {
    TKL_ZLL_SCANE_FOR_JOIN          = 0, ///< zll scan because of touchlink.
    TKL_ZLL_SCAN_FOR_LEAVE          = 1, ///< zll scan because of "reset to factory new".
    TKL_ZLL_SCAN_FOR_INDENTIFY      = 2, ///< zll scan because of identify.
} TKL_ZLL_SCAN_TYPE_E;

/*
 *  zll initiator scan status
 */
typedef enum {
    TKL_ZLL_SCAN_OK                 = 0, ///< zll scan start OK.
    TKL_ZLL_SCAN_BUSY               = 1, ///< zll scan start failed because of busy-bdb.
} TKL_ZLL_SCAN_STATUS_E;

/*
 *  zll initiator scan result
 */
typedef enum {
    TKL_ZLL_SCAN_RESULT_SUCESS      = 0, ///< zll scan OK, found device
    TKL_ZLL_SCAN_RESULT_NO_FOUND    = 1, ///< zll scan OK, no found device
    TKL_ZLL_TOUCH_REJOIN_SUCESS     = 2, ///< zll touch and rejoin OK
    TKL_ZLL_TOUCH_REJOIN_FAIL       = 3, ///< zll touch and rejoin FAIL
} TKL_ZLL_SCAN_RESULT_E;

/*
 *  zll initiator scan device information
 */
typedef struct{
    UINT8_T long_addr[8];               ///< zll device mac address
    UINT8_T ep_list[8];                 ///< zll endpoint list,max endpoints is 8
    ZG_NODE_TYPE_E type;                ///< zll device node type
    UINT16_T short_addr;                 ///< zll device short address
    UINT8_T ep_num;                     ///< zll endpoint numbers
    UINT8_T channel;                    ///< zll device channel
} TKL_ZLL_DEV_INFO_T;

/*
 *  zll initiator scan device information list
 */
typedef struct{
    UINT8_T dev_sums;
    TKL_ZLL_DEV_INFO_T *list;
} TKL_ZLL_DEV_LIST_T;

typedef VOID_T (*TKL_ZLL_TARGET_CMP_CB)(TKL_ZLL_TARGET_STATUS_E);
typedef VOID_T (*TKL_ZLL_INITIATOR_CMP_CB)(TKL_ZLL_SCAN_RESULT_E, TKL_ZLL_DEV_LIST_T*);
typedef VOID_T (*TKL_ZLL_IDENTIFY_REQ_CB)(UINT16_T durationS);
typedef VOID_T (*TKL_ZLL_RESET_TO_FN_CB)(VOID_T);
typedef OPERATE_RET (*TKL_ZLL_POWER_STRATETGY_CB)(BOOL_T);

typedef struct {
    TKL_ZLL_IDENTIFY_REQ_CB identify_request_cb;
    TKL_ZLL_RESET_TO_FN_CB reset_to_fn_cb;
} ZLL_EXT_CB_T;

/**
 * @brief time when zll commissioning rx is on in idle state
 *
 * @param[in]   time_ms: rx on time, 0: rx off when idle; 0xFFFFFFFF: rx on forever
 * @return rx on success or not
 */
BOOL_T tkl_zll_commissioning_rx_on_set(UINT32_T time_ms);

/**
 * @brief check zll commissioning is in progress to avoid some process conflicts 
 *
 * @param[in]   none
 * @return TRUE:in process
 */
BOOL_T tkl_zll_commissioning_in_process(VOID_T);

/**
 * @brief zll commissioning 3.0 test enable
 *
 * @param[in]   none
 * @return none
 */
VOID_T tkl_zll_commissioning_3_0_test_enable(VOID_T);

/**
 * @brief zll target touchlink init
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tkl_zll_target_touchlink_init(VOID_T);

// for server
/**
 * @brief zll target touchlink enable
 *
 * @param[in]   en: TRUE: enable, FALSE: disable
 * @return TRUE: enabel success
 */
BOOL_T tkl_zll_target_touchlink_enable(BOOL_T en);

/**
 * @brief zll target touchlink disable
 *
 * @param[in]   none
 * @return TRUE: enabel success
 */
BOOL_T tkl_zll_target_touchlink_is_enable(VOID_T);

/**
 * @brief zll target get rssi threshold
 *
 * @param[in]   none
 * @return rssi value
 */
INT8_T tkl_zll_target_rssi_threshold_get(VOID_T);

/**
 * @brief zll target set rssi threshold
 *
 * @param[in]   rx_rssi: rssi value
 * @return none
 */
VOID_T tkl_zll_target_rssi_threshold_set(INT8_T rx_rssi);

/**
 * @brief zll target no touchlink for non-factory new device
 * this function will cause an nfn device to refuse network start/join/update requests and 
 * thus to forbid commissioning by touchlinking.
 * this can be useful to restrict touchlink stealing.
 *
 * @param[in]   none
 * @return none
 */
BOOL_T tkl_zll_target_no_reset_for_non_factory_new(VOID_T);

/**
 * @brief no reset for non-factory new device
 * this function will cause an NFN device on a centralized security network to
 * a touchlink reset-to-factory-new request from a remote device.
 *
 * @param[in]   none
 * @return none
 */
BOOL_T tkl_zll_target_no_touchlink_for_non_factory_new(VOID_T);

/**
 * @brief zll target register complete callback
 *
 * @param[in]   cb: pointe to callback
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tkl_zll_target_register_complete_cb(TKL_ZLL_TARGET_CMP_CB cb);

/**
 * @brief 
 * 
 * @param cb 
 * @return OPERATE_RET 
 */
OPERATE_RET tkl_zll_target_register_identify_req_cb(TKL_ZLL_IDENTIFY_REQ_CB cb);

/**
 * @brief 
 * 
 * @param cb 
 * @return OPERATE_RET 
 */
OPERATE_RET tkl_zll_target_register_reset_to_fn_cb(TKL_ZLL_RESET_TO_FN_CB cb);

//for client
/**
 * @brief zll initiator touchlink init
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tkl_zll_initiator_touchlink_init(VOID_T);

/**
 * @brief zll initiator get tx power
 *
 * @param[in]   tx_db: radio tx power
 * @return none
 */
INT8_T tkl_zll_initiator_tx_power_get(VOID_T);

/**
 * @brief zll initiator set tx power
 *
 * @param[in]   tx_db: radio tx power
 * @return none
 */
VOID_T tkl_zll_initiator_tx_power_set(INT8_T tx_db);

/**
 * @brief zll initiator scan stop
 *
 * @param[in]   none
 * @return none
 */
VOID_T tkl_zll_initiator_scan_stop(VOID_T);

/**
 * @brief zll initiator scan start
 *
 * @param[in]   type: scan type
 * @return scan status
 */
TKL_ZLL_SCAN_STATUS_E tkl_zll_initiator_scan_start(TKL_ZLL_SCAN_TYPE_E type);

/**
 * @brief zll initiator register complete callback
 *
 * @param[in]   cb: pointe to callback
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tkl_zll_initiator_register_complete_cb(TKL_ZLL_INITIATOR_CMP_CB cb);



#ifdef __cplusplus
}
#endif
#endif
